<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Alex
 * Created Time: 2019/6/23 9:03
 * Modified Time: 2019/6/23 9:03
 * Version: 2.0
 */
namespace App\Modules\Member\Repositoryies;
use App\Exceptions\NotFoundException;
use App\Models\Member;
use App\Modules\Member\Criterias\getMemberInfoCriteria;
use Flower\Core\Repository\Eloquent\BaseRepository;
use Flower\Core\Repository\Exceptions\ValidatorException;
use Illuminate\Support\MessageBag;

class MemberRepository extends BaseRepository
{
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return Member::class;
    }

    /**
     * 会员剩余积分是否满足给定积分
     * @param  string  $member_id  会员ID
     * @param  float   $coin       积分
     * @return boolean  是否满足
     */
    public function isHaveCoin($member_id,$coin = 0.0)
    {
        $member = $this->asStatus()->find($member_id,['coin']);
        if(!$member){
            return false;
        }
        if($member['coin'] >= $coin){
            return true;
        }
        return false;
    }


    public function updateCoin($memberId,$coin){
    	$member = $this->find($memberId);
    	$member->coin += $coin;
    	return $member->save();
    }



	/**
	 * 用户是否存在
	 * @param $memberId
	 * @return mixed
	 * @throws NotFoundException
	 */
    public function isExisted($memberIdOrOpenId,$isMemberId=true){
    	$where = $isMemberId ? ['id'=>$memberIdOrOpenId] : ['open_id'=>$memberIdOrOpenId];
    	$memberInfo = $this->model->where($where)->first();
    	if(empty($memberInfo)){
    		throw new NotFoundException(['msg'=>'用户不存在！']);
	    }
    	return $memberInfo;
    }

	/**
	 * 是否有足够的县令币进行预约
	 * @param $memberId
	 * @return mixed
	 * @throws NotFoundException
	 */
    public function isHaveCoinPayAppointment($memberId){
    	$memberInfo = $this->find($memberId);
    	if($memberInfo->coin < 1){
    		throw new NotFoundException(['msg'=>'对不起，您的余额不足1.00县令币，请先充币！']);
	    }
    	return $memberInfo;
    }
//    /**
//     * 理发师是工作日期
//     */
//    public function onDutyTimes($requestData){
//		new
//    }

	/**
	 * 根据用户手机号查询用户是否是会员
	 * @param $phone
	 * @return mixed
	 */
		public function isHasMember($phone){
    	    return $this->findWhere(['mobile'=> $phone])->first();
		}

	/**
	 * 检测用户名是否存在
	 * @param $memberName
	 * @return mixed
	 * @throws NotFoundException
	 */
	public function checkMemberName($memberName){
		$member = $this->findWhere(['name'=>$memberName])->first();
		if(empty($member)){
			throw new NotFoundException(['msg'=>'用户名不存在！']);
		}
		return $member;
	}

	/**
	 * 获取用户信息
	 * @param $memberId
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getMemberInfoById($memberId){
		return $this->pushCriteria(new getMemberInfoCriteria($memberId))->first();
	}


	public function getAmountRate($storeId,$date,$compareDate){

	}
}